<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用最新Thinkphp5助手函数特性实现单字母函数M D U等简写方式
 * ============================================================================
 * Author: IT宇宙人     
 * Date: 2015-09-09
 */
namespace app\admin\controller;
use app\admin\logic\SaleLogic;
use think\Loader;
use think\Page;
use think\Db;

class Sale extends Base {
    /**
     *  拍卖分类列表
    */
    public function catelist()
    {
        $SaleLogic = new SaleLogic();
        $cat_list = $SaleLogic->sale_cat_list();
        $this->assign('cat_list',$cat_list);
        return $this->fetch();
    }

    /**
     * 添加修改商品分类
     * 手动拷贝分类正则 ([\u4e00-\u9fa5/\w]+)  ('393','$1'), 
     * select * from tp_sale_cate where id = 393
        select * from tp_sale_cate where parent_id = 393
        update tp_sale_cate  set parent_id_path = concat_ws('_','0_76_393',id),`level` = 3 where parent_id = 393
        insert into `tp_sale_cate` (`parent_id`,`name`) values 
        ('393','时尚饰品'),
    */
    public function addcate()
    {
        $SaleLogic = new SaleLogic(); 
        if(IS_POST){
            // 数据验证 
            $validate = \think\Loader::validate('Addcate');
            if(!$validate->batch()->check(input('post.')))
            {                        
                $error = $validate->getError();
                $error_msg = array_values($error);
                $return_arr = array(
                    'status' => -1,
                    'msg' => $error_msg[0],
                    'data' => $error,
                );
                $this->ajaxReturn($return_arr);
            }else{
                $data = input('post.');
                $data['name'] = trim(input('post.name')); 
                $data['sort_order'] = trim(input('post.sort')); 
                $info = DB::name('sale_cate')->insertGetId($data);
                if($info){
                    $SaleLogic->refresh_cat($info);
                    $return_arr = array(
                        'status' => 1,
                        'msg'   => '操作成功',
                        'data'  => array('url'=>U('admin/sale/catelist')),
                    );
                    $this->ajaxReturn($return_arr);
                }else{
                    $return_arr = array('status' => -1,'msg'   => '操作失败','data'  => '');
                    $this->ajaxReturn($return_arr);     
                }
            }
        }else{  
            $sale_cate_info = DB::name('sale_cate')->where('id='.I('GET.id',0))->find();
            $all_type = M('sale_cate')->where("level<3")->getField('id,name,parent_id');
            if(!empty($all_type)){
                $parent_id = empty($sale_cate_info) ? I('parent_id',0) : $sale_cate_info['parent_id'];
                $all_type = $SaleLogic->getCatTree($all_type);
                $cat_select = $SaleLogic->exportTree($all_type,0,$parent_id);
                $this->assign('cat_select',$cat_select);
            }
            return $this->fetch();
        }
    }

    /**
     * 修改拍卖分类
    */
    public function editcate(){
        $SaleLogic = new SaleLogic(); 
        if(IS_POST){
            $data = input('post.');
            $id = input('post.id');
            if($data['parent_id'] == $id){
                $return_arr = array('status' => -1,'msg'   => '上级分类不能为自己','data'  => '');
                $this->ajaxReturn($return_arr);  
            }elseif($data['parent_id'] == 0){
                $data['parent_id_path'] = $data['parent_id']."_".$id;
                $data['level'] = 1;
            }else{
                $parent_id = DB::name('sale_cate')->where('id',$data['parent_id'])->find();
                if($parent_id){
                    $data['parent_id_path'] = $parent_id['parent_id_path']."_".$id;
                    $data['level'] = $parent_id['level']+1;
                }
            }
            $info = DB::name('sale_cate')->where('id',$id)->update($data);
            if($info){
                $return_arr = array(
                    'status' => 1,
                    'msg'   => '操作成功',
                    'data'  => array('url'=>U('admin/sale/catelist')),
                );
                $this->ajaxReturn($return_arr);
            }else{
                $return_arr = array('status' => -1,'msg'   => '操作失败','data'  => '');
                $this->ajaxReturn($return_arr);     
            }
        }else{  
            $sale_cate_info = DB::name('sale_cate')->where('id='.I('GET.id',0))->find();
            $this->assign('cate',$sale_cate_info);
            $all_type = M('sale_cate')->where("level<3")->getField('id,name,parent_id');
            if(!empty($all_type)){
                $parent_id = empty($sale_cate_info) ? I('parent_id',0) : $sale_cate_info['parent_id'];
                $all_type = $SaleLogic->getCatTree($all_type);
                $cat_select = $SaleLogic->exportTree($all_type,0,$parent_id);
                $this->assign('cat_select',$cat_select);
            }
            return $this->fetch();
        }
    }

    /**
     *删除分类
    */
    public function delsalecate(){
        $ids = I('post.ids','');
        empty($ids) &&  $this->ajaxReturn(['status' => -1,'msg' =>"非法操作！",'data'  =>'']);
        // 判断子分类
        $count = Db::name("sale_cate")->where("parent_id = {$ids}")->count("id");
        $count > 0 && $this->ajaxReturn(['status' => -1,'msg' =>'该分类下还有分类不得删除!']);
        // 判断是否存在商品
        $goods_count = Db::name('sale')->where("catid = {$ids}")->count('1');
        $goods_count > 0 && $this->ajaxReturn(['status' => -1,'msg' =>'该分类下有分类不得删除!']);
        // 删除分类
        DB::name('sale_cate')->where('id',$ids)->delete();
        $this->ajaxReturn(['status' => 1,'msg' =>'操作成功','url'=>U('Admin/sale/catelist')]);
    }

    /**
     *  拍卖商品列表
    */
    public function goodlist(){
        $keyword = I('keyword');
        if(!empty(I('state'))){
            if(I('state') == 'shen'){
                $msg = "";
            }elseif(I('state') == '2'){
                $msg['state'] = 1;
            }elseif(I('state') == '3'){
                $msg['state'] = 0;
            }else{
                $msg['state'] = 2;
            }
        }else{
            $msg = "";
        }
        $count =  Db::name("sale")->alias('a')->join('sale_cate b','a.parent_id_path = b.parent_id_path')->field('b.name,a.*')->where('a.sname','like',"%".$keyword."%")->where($msg)->count();
        $Page = $pager = new Page($count,10);    
        $cate = Db::name("sale")->alias('a')->join('sale_cate b','a.parent_id_path = b.parent_id_path')->field('b.name,a.*')->where('a.sname','like',"%".$keyword."%")->where($msg)->order("id desc")->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($cate as $key => $value) {
           $cate[$key]['nickname'] = $this->username($value['uid']);
        }
        $show  = $Page->show();   
        $this->assign('cate',$cate);   
        $this->assign('pager',$pager);
        $this->assign('show',$show);
        return $this->fetch();
    }

    public function username($id){
        $uid = DB::name('users')->field('nickname')->where('user_id',$id)->find();
        return $uid['nickname'];
    }
    
}